package com.example.forum.services.impl;

import com.example.forum.common.AppResult;
import com.example.forum.common.ResultCode;
import com.example.forum.dao.ArticleMapper;
import com.example.forum.dao.ArticleReplyMapper;
import com.example.forum.exception.ApplicationException;
import com.example.forum.model.ArticleReply;
import com.example.forum.services.IArticleReplyService;
import com.example.forum.utils.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description: 帖子评论的 service 层实现类
 * User: DELL
 * Date: 2023-09-26
 * Time: 10:57
 */
@Slf4j
@Service
public class ArticleReplyServiceImpl implements IArticleReplyService {

    @Resource
    private ArticleReplyMapper articleReplyMapper;
    @Resource
    private ArticleMapper articleMapper;

    @Override
    public void create(ArticleReply articleReply) {
        // 参数校验
        if (articleReply == null || articleReply.getArticleId() == null
                || articleReply.getPostUserId() == null
                || StringUtil.isEmpty(articleReply.getContent())) {
            // 打印日志
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            // 抛出异常
            throw new ApplicationException(AppResult.fail(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        // 设置默认值
        articleReply.setLikeCount(0);
        articleReply.setState((byte) 0);
        articleReply.setDeleteState((byte) 0);
        Date date = new Date();
        articleReply.setCreateTime(date);
        articleReply.setUpdateTime(date);
        // 调用 dao层
        int row = articleReplyMapper.insertSelective(articleReply);
        if (row != 1) {
            // 打印日志
            log.warn("新增帖子回复异常,受影响的行数 row = " + row);
            // 抛出异常
            throw new ApplicationException(AppResult.fail(ResultCode.ERROR_SERVICES));
        }
        // 将对应帖子的回复数加一
        row = articleMapper.addOneReplyCountById(articleReply.getArticleId());
        if (row != 1) {
            // 打印日志
            log.warn("新增帖子回复时更新帖子的回复数异常,受影响的行数 row = " + row + ",article id = " + articleReply.getArticleId());
            // 抛出异常
            throw new ApplicationException(AppResult.fail(ResultCode.ERROR_SERVICES));
        }
        // 打印日志
        log.info("新增帖⼦回复成功, user Id = " + articleReply.getPostUserId() +
                ", article Id = " + articleReply.getArticleId() + ", articleReply Id = " +
                articleReply.getId());
    }

    @Override
    public List<ArticleReply> selectByArticleId(Long articleId) {
        // 参数校验
        if (articleId == null || articleId <= 0) {
            // 打印日志
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            // 抛出异常
            throw new ApplicationException(AppResult.fail(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        // 调用 dao,查询并返回
        return articleReplyMapper.selectByArticleId(articleId);
    }
}
